/**
 * 组件配置公共js
 */
import ComponentStyle from "@/views/modules/design/right-component/common/component-style.vue";
import ScreenApi from "@/api/screen-api";
import ScreenDataModelApi from "@/api/screen/core/screen-data-model";
import {Util} from "@lowcode/jpaas-base-lib";
import StaticDataDialog from "@/views/modules/design/dialog/static-data-dialog.vue";
import SettingDialog from "@/views/modules/design/dialog/setting-dialog.vue";


const ComponentDef = {
	props:["config","screenConfig","curComponent"],
	components:{
        ComponentStyle
	},
	provide(){
		return{
			config:this.config,
			getPubParams:this.getPubParams,
			getPublishOptions:this.getPublishOptions,
			paramsOptions:this.paramsOptions,
			constantField:this.constantField,
			urlParams:this.screenConfig?this.screenConfig.params:[]
		}
	},
	data() {
		return {
			//参数
			paramsOptions:[],
			//常量
			constantField:[],
			dataModel:"",
			//数据模型的配置 维度、度量、参数
			modelConfig: {
				//维度
				dimensions: [],
				// 度量
				measures: [],
			},
		}
	},
	created() {
		this.loadConstants();
	},
	methods: {
		commonI18n(name,text,key){
			return this.$ti18(name,text,"componentStyleLang",key);
		},
		loadConstants() {
			ScreenApi.getConstantItem().then((data) => {
				this.constantField = this.constantField.concat(
					data.map((item) => {
						return { label: item.val, value: item.key }
					})
				)
			})
		},
		//获取组件发布参数
		getPubParams(key) {
			let components = this.getPublishComponents();
			let component = {};
			let found = false;
			for (let i = 0; i < components.length; i++) {
				component = components[i];
				if (component.config.key == key) {
					found = true;
					break;
				}
			}
			let ary = [];
			if (!found) {
				return ary;
			}
			let params = component.config.advancedConf.publish.paramMapping;
			for (let i = 0; i < params.length; i++) {
				let obj = params[i];
				let o = {value: obj.key, label: obj.label};
				ary.push(o);
			}
			return ary;
		},
		//获取配置发布数据的控件
		getPublishComponents() {
		    if(!this.screenConfig){
		        return [];
            }
			let ary = this.screenConfig.components.filter(item => {
				if(!item.config || !item.config.advancedConf){
					return false;
				}
				let pubSetting = item.config.advancedConf.publish;
				if (pubSetting && pubSetting.paramMapping.length > 0) {
					return true;
				}
				return false;
			});
			return ary;
		},
		getPublishOptions() {
			let ary = this.getPublishComponents();
			let rtn = [];
			for (let i = 0; i < ary.length; i++) {
				let config = ary[i].config;
				let obj = {value: config.key, label: config.title};
				rtn.push(obj);
			}
			return rtn;
		},
		getDataModel(value,isInit) {
			if (value) {
				ScreenDataModelApi.get(value).then(res => {
					let dataMode=res.data;
					if(!dataMode.modelConfig){
						return;
					}
					this.modelConfig = JSON.parse(dataMode.modelConfig);
					if(dataMode.sourceType=="dataSource" && (dataMode.mode=="table" || dataMode.sqlModel)){ //来源于数据源
						this.tables = JSON.parse(dataMode.tables);
						this.getTableName();
					}
					if(isInit){
						//拖拽里面清空
						if(this.config.dataSources.dimensions){
							this.config.dataSources.dimensions = [];
						}
						this.config.dataSources.measures = [];
						if(this.config.dataSources.measures2){
							this.config.dataSources.measures2 = [];
						}
						if(this.config.dataSources.colorLegends){
							this.config.dataSources.colorLegends = [];
						}
					}
					if (dataMode.dataSource) {
						this.dataSource = JSON.parse(dataMode.dataSource).value;
					}
					this.dataModel = JSON.stringify({
						text: this.config.dataSources.dataModelName || dataMode.name,
						value: this.config.dataSources.dataModel
					});
					this.getParamsOptions();
				})
			}
		},
		getTableName() {
			//维度新增添加tableName
			for (let obj of this.modelConfig.dimensions) {
				if (obj.fields) {
					for (let obj_children of obj.fields) {
                        obj_children["tableName"]=obj.tableName;
					}
				}
			}
			//度量新增添加tableName
			for (let obj of this.modelConfig.measures) {
				if (obj.fields) {
					for (let obj_children of obj.fields) {
                        obj_children["tableName"]=obj.tableName;
					}
				}
			}
		},
		getParamsOptions(){
			if (this.modelConfig) {
				let params = this.modelConfig.params;
				if(!params){
					params=[];
				}
				for (let i = 0; i < params.length; i++) {
					let param = params[i];
					for (let j = 0; j < param.fields.length; j++) {
						this.paramsOptions.push({
							label: param.fields[j].fieldLabel,
							value: param.tableName + "." + param.fields[j].fieldName,
							comparison: param.fields[j].comparison
						});
					}
				}
				//地图需要默认增加一个地图类型{label:"地图类型", value:"mapType_"}
				if(this.config.chartType=="map"){
					this.paramsOptions.push({
						label:"地图类型",
						value:"mapType_"
					});
				}
			}
		},
		//上移
		toUp(rows,index){
			if(rows.length<=1 || index==0){
				return;
			}
			rows[index-1] = rows.splice(index, 1, rows[index-1])[0];
		},
		//下移
		toDown(rows,index){
			if(rows.length<=1 || index==rows.length-1){
				return;
			}
			rows[index+1] = rows.splice(index, 1, rows[index+1])[0];
		},
		/**
		 * 设置静态数据
		 */
		setStaticData(){
			let self=this;
			Util.open({
				component: StaticDataDialog,
				curVm: this,
				widthHeight: ['800px', '500px'],
				title:this.commonI18n('staticData','静态数据'),
				zIndex: 10000,
				data:{
					config:this.config,
					curComponent:this.curComponent
				}
			}, function (action, data) {
				if (action != 'ok') return;
				self.config.dataSources.staticData=data;
			});
		},
        getImage(url){
            return import.meta.env.JPAAS_BASE_URL+url;
        },
        //图标
        getIcon(obj) {
            if (obj.dataType == 'string') {
                return 'icon-lineField-String';
            } else if (obj.dataType == 'date') {
                return 'icon-lineriqi2';
            } else  {
                return 'icon-lineshuzi';
            }
        },
        setSetting(item,index,type){
            let self=this;
            Util.open({
                component: SettingDialog,
                curVm: this,
                widthHeight: ['600px', '420px'],
                title:"设置",
                zIndex: 10000,
                data:{
                    settingConfig:item,
                    type:this.curComponent.type
                }
            }, function (action, data) {
                if (action != 'ok') return;
                self.config.dataSources[type][index]=data;
            });
        }
	}

}

export default ComponentDef;
